# check env
# export -p
IS_SILENT=${1}
if [ ! ${1} ] ;then
    IS_SILENT=0
fi
PLATFORM=$(uname)
IS_MAC=$(uname -a|grep Darwin|wc -l)
IS_LINUX=$(uname -a|grep Linux|wc -l)
IS_UBUNTU=$(uname -a|grep Ubuntu|wc -l)
IS_CENTOS=$(uname -a|grep Cent|wc -l)

DATE_TIME_NOW_STR="`date +%Y%m%d%H%M`"
DATE_TIME_NOW_STR="TTT"

Z6X_USER_NAME=$(whoami)

init_color() {
  # Use colors, but only if connected to a terminal, and that terminal
  # supports them.
  if which tput >/dev/null 2>&1; then
      ncolors=$(tput colors)
  fi
  if [ -t 1 ] && [ -n "$ncolors" ] && [ "$ncolors" -ge 8 ]; then
    RED="$(tput setaf 1)"
    GREEN="$(tput setaf 2)"
    YELLOW="$(tput setaf 3)"
    BLUE="$(tput setaf 4)"
    BOLD="$(tput bold)"
    NORMAL="$(tput sgr0)"
  else
    RED=""
    GREEN=""
    YELLOW=""
    BLUE=""
    BOLD=""
    NORMAL=""
  fi
}

z6h() {
    if ! [ ${IS_SILENT} = 1 ]; then
        printf "\n${BLUE}--------------------${1}${NORMAL}"
    fi
}

z6p() {
    if ! [ ${IS_SILENT} = 1 ]; then
        printf "\n${1}"
    fi
}

z6e() {
    printf "\n${RED}!!!:ERROR--${1}${NORMAL}"
}

check_cmd() {
    # z6p "check_cmd_"${1}
    if ! [ -x "$(command -v ${1})" ] ; then
        z6e "[${1}]command not install"
        exit 1
    else
        z6p "comand-path:"$(command -v ${1})
    fi
}

check_env() {
    z6p "PLATFORM="${PLATFORM}
    z6p "HOME= "${HOME}
    z6p "Z6X_USER_NAME= "${Z6X_USER_NAME}
    z6p "DATE_TIME_NOW_STR= "${DATE_TIME_NOW_STR}
    z6p "shells on this host:"
    cat /etc/shells|grep "/"
    z6p "CURRENT_SHELL= "${SHELL}
    z6p "ZSH:${ZSH} ZSH_CUSTOM:${ZSH_CUSTOM}"
}

check_cmds() {
    check_cmd "sh"
    check_cmd "chsh"
    check_cmd "curl"
    check_cmd "git"
}

check() {
    z6h "Step:check"
    check_env
    check_cmds
}

bak() {
    z6h "Step:bak"
    if [ -f "${HOME}/.zshrc" ] ; then
        cp ${HOME}/.zshrc ${HOME}/.zshrc.${DATE_TIME_NOW_STR}.bak
        z6p "2-2:.zshrc exists and bak"
    fi
}
install_omz() {
    z6p "install_omz"
    OH_MY_ZSH_URL="https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh"
    sh -c "$(curl -fsSL ${OH_MY_ZSH_URL})"
    if [ ! -n "${ZSH}" ] ; then
        ZSH=${HOME}/.oh-my-zsh
    fi
    if [ -n "${ZSH}" ] ; then
        ZSH_CUSTOM=${ZSH}/custom
        z6p "${ZSH_CUSTOM}"
    else
        z6e "oh-my-zsh install error"
        exit 1
    fi

    if [ -n "${ZSH_CUSTOM}" ] ; then
        ZSH_CUSTOM_PLUGINS=${ZSH_CUSTOM}/plugins
        z6p "${ZSH_CUSTOM_PLUGINS}"
    else
        z6e "oh-my-zsh install error h"
        exit 1
    fi

    if [ -d "${ZSH_CUSTOM_PLUGINS}" ] ; then
        z6p "zsh custom plugins dir exists, install plugin"
        install_omz_plugin_auto_sugg
        install_omz_plugin_synax_hightlighting
    fi
    change_shell
    printf "\n${GREEN} ???${NORMAL}"
}

change_shell() {
    # If this user's login shell is not already "zsh", attempt to switch.
    TEST_CURRENT_SHELL=$(expr "$SHELL" : '.*/\(.*\)')
    if [ "$TEST_CURRENT_SHELL" != "zsh" ]; then
        # If this platform provides a "chsh" command (not Cygwin), do it, man!
        if hash chsh >/dev/null 2>&1; then
            printf "${BLUE}Time to change your default shell to zsh!${NORMAL}\n"
            chsh -s $(grep /zsh$ /etc/shells | tail -1)
            zsh -l
        # Else, suggest the user do so manually.
        else
            printf "I can't change your shell automatically because this system does not have chsh.\n"
            printf "${BLUE}Please manually change your default shell to zsh!${NORMAL}\n"
            exit 1
        fi
    fi
}

install_omz_plugin_auto_sugg() {
    ZSH_CP_AUTO_SUG=${ZSH_CUSTOM_PLUGINS}/zsh-autosuggestions

    if [ ! -d "${ZSH_CP_AUTO_SUG}" ] ; then
        z6p ${ZSH_CUSTOM_PLUGINS}
        env git clone git://github.com/zsh-users/zsh-autosuggestions $ZSH_CP_AUTO_SUG
    else
        z6p "zsh-autosuggestions installed"
    fi

    # config auto-suggest
    if [ -f "${HOME}/.zshrc" ] ; then
        z6p "sed replace\n"
        if [ "${PLATFORM}" = "Darwin" ] ; then
            z6p "MAC sed -i command diffs vimrc"
            sed -i "" "s/^  git/  zsh-autosuggestions git/g" ${HOME}/.zshrc
        else
            sed -i "s/^  git/  zsh-autosuggestions git/g" ${HOME}/.zshrc
        fi
        # cat ${HOME}/.zshrc 
        # source ${HOME}/.zshrc
    fi

    if [ -f "${ZSH_CP_AUTO_SUG}/zsh-autosuggestions.zsh" ] ; then
        if [ "${PLATFORM}" = "Darwin" ] ; then
            z6p "MAC sed -i command diffs"
            sed -i "" "s/fg=8/fg=10/g" ${ZSH_CP_AUTO_SUG}/zsh-autosuggestions.zsh
        else
            sed -i "s/fg=8/fg=10/g" ${ZSH_CP_AUTO_SUG}/zsh-autosuggestions.zsh
        fi
    fi
}

install_omz_plugin_synax_hightlighting() {
    ZSH_CP_P=${ZSH_CUSTOM_PLUGINS}/zsh-syntax-highlighting

    if [ ! -d "${ZSH_CP_P}" ] ; then
        z6p ${ZSH_CUSTOM_PLUGINS}
        env git clone git://github.com/zsh-users/zsh-syntax-highlighting $ZSH_CP_P
    else
        z6p "zsh-syntax-highlighting installed"
    fi

    # config auto-suggest
    if [ -f "${HOME}/.zshrc" ] ; then
        z6p "sed replace\n"
        if [ "${PLATFORM}" = "Darwin" ] ; then
            z6p "MAC sed -i command diffs vimrc"
            sed -i "" "s/^  zsh-autosuggestions git/  zsh-autosuggestions git zsh-syntax-highlighting/g" ${HOME}/.zshrc
        else
            z6p "linux"
            sed -i "s/^  zsh-autosuggestions git/  zsh-autosuggestions git zsh-syntax-highlighting/g" ${HOME}/.zshrc
        fi
        # cat ${HOME}/.zshrc 
        # source ${HOME}/.zshrc
    fi
}

install() {
    z6h "Step:install"
    install_omz
}


main() {
    # Only enable exit-on-error after the non-critical colorization stuff,
    # which may fail on systems lacking tput or terminfo
    set -e
    init_color
    check
    bak
    install
    change_shell
    printf "\n${GREEN} DONE!${NORMAL}\n"
}
main